package com.sunda.spmsorder.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.sunda.spmsorder.mapper.OrderTraceMapper;
import com.sunda.spmsorder.service.IOrderTraceService;
import com.sunda.spmsorder.service.IOrderWerksService;
import com.sunda.spmsorder.service.IOrderWorkshopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: spms
 * @description:
 * @author: Wayne Wu
 * @create: 2022-01-07 16:22
 **/
@Service
public class OrderTraceImpl implements IOrderTraceService {

    @Autowired
    IOrderWerksService iOrderWerksService;

    @Autowired
    IOrderWorkshopService iOrderWorkshopService;

    @Autowired
    OrderTraceMapper orderTraceMapper;


    /** SPMS系统有箱码但无操作日志且自管仓无库存，则认为自管仓待到货；日期参数为箱码在SPMS创建起止时间；*/
    @Override
    public List<Map<String, Object>> getWaitingForArrivalBoxList(String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(beginDate)){
            beginDate = getMonthsAgo();
        }
        if (StringUtils.isEmpty(endDate)){
            endDate = sdf.format(new Date());
        }
        return orderTraceMapper.getWaitingForArrivalBoxList(beginDate, endDate);
    }

    /** 自管仓内的箱码列表，直接从储位库存获取，即自管仓库内箱码；werks = CN01 */
    @Override
    public List<Map<String, Object>> getCN01BoxList(String werks, String whsLocationCode, String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(beginDate)){
            beginDate = getMonthsAgo();
        }
        if (StringUtils.isEmpty(endDate)){
            endDate = sdf.format(new Date());
        }
        return orderTraceMapper.getCN01BoxList(werks, whsLocationCode, beginDate, endDate);
    }

    /** 海运/转储在途库存，从南沙箱码出库日志提取每个箱码最新一条，OPERATION_TYPE = 3, 16, 20，22 即为在途； */
    @Override
    public List<Map<String, Object>> getOverseaOnTheWayBoxList(String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(beginDate)){
            beginDate = getMonthsAgo();
        }
        if (StringUtils.isEmpty(endDate)){
            endDate = sdf.format(new Date());
        }
        return orderTraceMapper.getOverseaOnTheWayBoxList(beginDate, endDate);
    }

    /** 海外到货，按箱码到货日志获取；OPERATION_TYPE = 21 表示海外已到货 */
    @Override
    public List<Map<String, Object>> getOverseaArrivalBoxList(String beginDate, String endDate) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if (StringUtils.isEmpty(beginDate)){
            beginDate = getMonthsAgo();
        }
        if (StringUtils.isEmpty(endDate)){
            endDate = sdf.format(new Date());
        }
        return orderTraceMapper.getOverseaArrivalBoxList(beginDate, endDate);
    }

    String getMonthsAgo(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.MONTH, -6);
        Date date = calendar.getTime();
        return sdf.format(date);
    }
}


